package com.ecoolex.hotel.saas.core.manager;

import com.ecoolex.hotel.saas.core.dao.PaymentOrderRecordDAO;
import com.ecoolex.hotel.saas.entity.PaymentOrder;
import com.ecoolex.hotel.saas.entity.PaymentOrderRecord;
import com.ecoolex.hotel.saas.entity.PaymentOrderRecord.Column;
import com.ecoolex.hotel.saas.entity.PaymentOrderRecordExample;
import com.ecoolex.hotel.saas.enums.PaymentOrderRecordOperType;
import com.ecoolex.hotel.saas.enums.PaymentOrderRecordResultStatus;

import static com.ecoolex.hotel.saas.entity.PaymentOrderRecord.NOT_DELETED;

import java.time.Instant;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PaymentOrderRecordManager {
	
    @Autowired
    private PaymentOrderRecordDAO dao;
    
    public PaymentOrderRecord create(PaymentOrder order, Integer paymentType, PaymentOrderRecordOperType operType) {
        PaymentOrderRecord record = new PaymentOrderRecord();
        record.setPaymentOrderId(order.getPaymentOrderId());
        record.setPaymentType(paymentType);
        record.setOperType(operType.real());
        return record;
    }

    public void add(PaymentOrderRecord record, String createUser) {
        record.setDeleted(NOT_DELETED);
        record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.insert(record);
    }

    public List<PaymentOrderRecord> queryByOrderId(Integer paymentOrderId) {
        PaymentOrderRecordExample example = new PaymentOrderRecordExample();
        example.createCriteria().andPaymentOrderIdEqualTo(paymentOrderId);
        return dao.selectByExample(example);
    }

    /*
    获取上一次支付申请记录
     */
    public PaymentOrderRecord getLastPayApplyRecord(Integer paymentOrderId) {
        PaymentOrderRecordExample example = new PaymentOrderRecordExample();
        example.createCriteria()
                .andPaymentOrderIdEqualTo(paymentOrderId)
                .andOperTypeEqualTo(PaymentOrderRecordOperType.ORDER_APPLY.real())
                .andResultStatusEqualTo(PaymentOrderRecordResultStatus.SUCCEED.real())
                .andDeletedEqualTo(NOT_DELETED);
        example.orderBy(Column.createTime.desc());
        return dao.selectOneByExample(example);
    }

    /*
    获取上一次退款申请记录
     */
    public PaymentOrderRecord getLastRefundApplyRecord(Integer paymentOrderId) {
        PaymentOrderRecordExample example = new PaymentOrderRecordExample();
        example.createCriteria()
                .andPaymentOrderIdEqualTo(paymentOrderId)
                .andOperTypeEqualTo(PaymentOrderRecordOperType.REFUND_APPLY.real())
                .andResultStatusEqualTo(PaymentOrderRecordResultStatus.SUCCEED.real())
                .andDeletedEqualTo(NOT_DELETED);
        example.orderBy(Column.createTime.desc());
        return dao.selectOneByExample(example);
    }
}